package com.furentech.iotserver.fa.sinopec.sinopec_processor;


import com.alibaba.fastjson.JSON;
import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import com.furentech.iotserver.fa.sinopec.SinopecRestRequest;
import com.furentech.iotserver.fa.utils.BCD8421Operater;
import com.furentech.iotserver.fa.utils.HexStringUtils;
import io.netty.channel.Channel;
import org.springframework.stereotype.Component;
import lombok.Data;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * FA E0 E0 5E 00 58
 *
 * 12
 * 01 02 06
 *
 * 01 00 04 13 20 00 00 29 22 26
 * 00
 * 00 09 E2
 * 00 01 BF
 * 02 36
 * 20 20 08 21 10 20 09
 * 20 20 08 21 10 20 09
 * 20 20 08 21 09 52 58
 * 20 20 08 21 09 52 58
 * 00 40 20
 * 00 40 20
 * 02
 * 02 37
 */



@Data
class NozzleStData2
{
    int COUNT;
    int St;
    int NZN;
    String ASN;
    int P_UNIT;
    int AMN;
    int VOL;
    int PRC;
    String NozUp;
    String ValveOpen;
    String ValveClose;
    String NozDown;
    String AvrFlowrate;
    String MaxFlowrate;
    int Type;
};

@Component
public class NozzleStData_Processor extends SinopecProcessorBase {

    NozzleStData2 decodeFormBytes(byte[] buf)
    {
        NozzleStData2 nozzleStData = new NozzleStData2();

        nozzleStData.setCOUNT(buf[1]);
        nozzleStData.setSt(buf[2]);
        nozzleStData.setNZN(buf[3]);
        nozzleStData.setASN(HexStringUtils.toHexString(buf, 4, 10));
        nozzleStData.setP_UNIT(buf[14]);
        nozzleStData.setAMN(((buf[15] & 0x00ff) << 16) + ((buf[16] & 0x00ff) << 8) + buf[17]);
        nozzleStData.setVOL(((buf[18] & 0x00ff) << 16) + ((buf[19] & 0x00ff) << 8) + buf[20]);
        nozzleStData.setPRC(((buf[21] & 0x00ff) << 8) + ((buf[22] & 0x00ff)));
        nozzleStData.setNozUp(HexStringUtils.toHexString(buf, 23, 7));
        nozzleStData.setValveOpen(HexStringUtils.toHexString(buf, 30, 7));
        nozzleStData.setValveClose(HexStringUtils.toHexString(buf, 37, 7));
        nozzleStData.setNozDown(HexStringUtils.toHexString(buf, 44, 7));
        nozzleStData.setAvrFlowrate(String.format("%02x%02x.%02x", buf[51], buf[52], buf[53]));
        nozzleStData.setMaxFlowrate(String.format("%02x%02x.%02x", buf[54], buf[55], buf[56]));
        nozzleStData.setType(buf[57]);

        return  nozzleStData;
    }



    @Override
    String actualGetJsonString(byte[] buf) {
        if (buf[1] == 0x02) {
            NozzleStData2 nozzleStData = decodeFormBytes(buf);
            String jsonstr = JSON.toJSONString(nozzleStData);
            return jsonstr;
        }
        return null;
    }

    @Override
    String actualGetReadableString(byte[] buf) {
        if (buf[1] == 0x02) {

        NozzleStData2 nozzleStData=decodeFormBytes(buf);
        return String.format(

                        "信息数:%d\r\n" +
                        "状态字:%d\r\n" +
                        "枪号:%d\r\n" +
                        "卡应用号:%s\r\n" +
                        "结算单位/方式:%d\r\n" +
                        "数额:%d\r\n" +
                        "升数:%d\r\n" +
                        "价格:%d\r\n" +
                        "提枪时间:%s\r\n" +
                        "开阀时间:%s\r\n" +
                        "关阀时间:%s\r\n" +
                        "挂枪时间:%s\r\n" +
                        "平均流速:%s\r\n" +
                        "最大流速:%s\r\n" +
                        "定量方式:%d\r\n",
                nozzleStData.COUNT,
                nozzleStData.St,
                nozzleStData.NZN,
                nozzleStData.ASN,
                nozzleStData.P_UNIT,
                nozzleStData.AMN,
                nozzleStData.VOL,
                nozzleStData.PRC,
                nozzleStData.NozUp,
                nozzleStData.ValveOpen,
                nozzleStData.ValveClose,
                nozzleStData.NozDown,
                nozzleStData.AvrFlowrate,
                nozzleStData.MaxFlowrate,
                nozzleStData.Type
                );
        }
        return null;

    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {

        String dateString = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString();
        byte[] dateBcdBuf = BCD8421Operater.string2Bcd(dateString);

        byte[] responseBuf = new byte[17];
        responseBuf[0] = SinopecFunCode.Ver_Info;
        System.arraycopy(dateBcdBuf, 0, responseBuf, 1, 7);

        VerInfo verInfo= SinopecRestRequest.getVerInfo();



        responseBuf[8] = (byte) (0x00ff & (verInfo.getBL_VER() / 256));
        responseBuf[9] = (byte) (0x00ff & (verInfo.getBL_VER() % 256));

        responseBuf[10] = (byte) (0x00ff & (verInfo.getADD_BL_VER() % 256));
        responseBuf[11] = (byte) (0x00ff & (verInfo.getDEL_BL_VER() % 256));
        responseBuf[12] = (byte) (0x00ff & (verInfo.getWH_VER() % 256));
        responseBuf[13] = (byte) (0x00ff & (verInfo.getPRC_VER() % 256));
        responseBuf[14] = (byte) (0x00ff & (verInfo.getSta_VER() % 256));

        responseBuf[15]=0;
        responseBuf[16]=0;

        return responseBuf;
    }
}

